<?php

/**
 *  签名算法
 *  该方法可以公用
 * @param int $timeStamp
 * @param string $appId
 * @param string $appSecret
 * @return string 返回签名数据
 */
function getSign($timeStamp, $appId, $appSecret)
{   
    $sign = "appId={$appId}&appSecret={$appSecret}&timeStamp={$timeStamp}";
    return strtoupper(md5($sign));
}

/**
 * post 业务请求
 * 该方法可以公用
 * @param string $url
 * @param string $jsonData
 * @param int $cid
 * @param string $appId
 * @param string $appSecret
 */
function http_post_json($url, $jsonData, $cid, $appId, $appSecret)
{
    // 1. 获取当前时间戳
    $timeStamp = time() * 1000;
    // 2. 获取签名
    $sign = getSign($timeStamp, $appId, $appSecret);
    // 3. 封装请求头
    $head = array(
        'Content-Type: application/json; charset=utf-8',
        'X-Ec-Cid: ' . $cid,
        'X-Ec-Sign: ' . $sign,
        'X-Ec-TimeStamp: ' . $timeStamp,
        'Content-Length: ' . strlen($jsonData)
    );

    // 3. 传入http 参数
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // https 支持 - 对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    // 请求数据放这里
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    // head
    curl_setopt($ch, CURLOPT_HTTPHEADER, $head);

    // 请求服务器
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // 组织，返回结果和响应码
    return array(
        $httpCode,
        $response
    );
}

/**
 * 该方法可以公用
 */
function doPost($url, $jsonData)
{
    $cid = 18600635; // 需要换成您自己的 企业id
    $appId = "939165456525164544"; // 需要换成您自己的 appId
    $appSecret = "rPBv229DhfkZn2bBkMa"; // 需要换成您自己的 appSecret
    return http_post_json($url, $jsonData, $cid, $appId, $appSecret);
}

// function testAddCustomer()
// {
//     // 1. 组装请求数据
//     $data = array(
//         'mobile'=>17611740979,
//     );
//     $jsonData = json_encode($data);

//     // 2. 请求服务器处理
//     $url = "https://open.workec.com/v2/customer/queryList";
//     list ($returnCode, $returnContent) = doPost($url, $jsonData);

//     // 3. 输出结果：处理您的结果
//     echo "code = " . $returnCode . "\n";
//     // echo "result = " . $returnContent . "\n";
//     var_dump(json_decode($returnContent));
// }
// 测试入口
// 6494186094

function testAddCustomer()
{
    // 1. 组装请求数据
    $data = array(
        "list"=>[ [
            "f_crm_id"=>"6494186094",
            "f_user_id"=>"18631379",
            "f_Starttime"=>"2022-11-04 10:16:00",
            "f_Endtime"=>"2022-11-04 10:16:40",
            "f_in_out_type"=>2,
            "f_Calltime"=>40,
            "f_callno"=>"18310765738",
            "f_calltono"=>"17611740979",
            "f_path"=>"https://www.jialilaw.com/0021.m4a"
        ]
        ]
    );
    $jsonData = json_encode($data);

    // 2. 请求服务器处理
    $url = "https://open.workec.com/v2/record/addTelRecord";
    list ($returnCode, $returnContent) = doPost($url, $jsonData);

    // 3. 输出结果：处理您的结果
    echo "code = " . $returnCode . "\n";
    // echo "result = " . $returnContent . "\n";
    var_dump(json_decode($returnContent));
}


testAddCustomer();
